package com.atguigu.tree;

public class ArrBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.preOrder();
        System.out.println("---------------------");
        arrBinaryTree.midOrder(0);
        System.out.println("---------------------");
        arrBinaryTree.postOrder(0);
    }
}

class ArrBinaryTree{
    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    public void preOrder(){
        preOrder(0);
    }

    public void preOrder(int index){
        // 打印当前节点
        System.out.println(arr[index]);

        // 打印左部
        int leftIndex=2*index+1;
        if(leftIndex<arr.length){
            preOrder(leftIndex);
        }

        // 打印右部
        int rightIndex=2*index+2;
        if(rightIndex<arr.length){
            preOrder(rightIndex);
        }
    }

    public void midOrder(int index){
        // 打印左部
        int leftIndex=2*index+1;
        if(leftIndex<arr.length){
            midOrder(leftIndex);
        }

        // 打印当前节点
        System.out.println(arr[index]);

        // 打印右部
        int rightIndex=2*index+2;
        if(rightIndex<arr.length){
            midOrder(rightIndex);
        }
    }

    public void postOrder(int index){
        // 打印左部
        int leftIndex=2*index+1;
        if(leftIndex<arr.length){
            postOrder(leftIndex);
        }

        // 打印右部
        int rightIndex=2*index+2;
        if(rightIndex<arr.length){
            postOrder(rightIndex);
        }

        // 打印当前节点
        System.out.println(arr[index]);
    }
}
